from django.shortcuts import render, HttpResponse


# Create your views here.

def index(reqest):
    return HttpResponse("ok")


def info(request):
    if request.method == "GET":
        return render(request, "info.html")

    if request.method == "POST":
        nickname = request.POST.get('nickname', "暂无昵称")
        province = request.POST.get('province', "省份")
        city = request.POST.get('city', "城市")
        district = request.POST.get('district', "区县")
        sex = request.POST.get('sex', "区县")
        customsign = request.POST.get('customsign', "区县")

        icon = request.FILES.get("icon")

        savepath = r'D:\PycharmProjects\newlife\static'
        with open(savepath + '\icon.jpg', 'wb') as f:
            for chunk in icon.chunks():
                f.write(chunk)

        return HttpResponse("nickname:{},province:{},city:{},district:{},sex:{},customsign:{}".format(nickname,
                                                                                                      province, city,
                                                                                                      district, sex,
                                                                                                      customsign))


def allUser(request):
    users = [
        {"name": "张三", "address": "北京", "phone": "13543434233"},
        {"name": "李四", "address": "上海", "phone": "18843434233"},
        {"name": "王五", "address": "广州", "phone": "16643434233"}]
    return render(request, 'allUser.html', {"users": users})


from .models import Student, Book


def testfun(request):
    # s1 = Student(name='张三')
    # s1.save()

    # b1 = Book(bookname="django", price=9.9, isbn='123456')
    # b1.save()
    # 通过类 来创建
    # Student.objects.create(name='李四')

    # 批量创建对象
    # s1 = Student(name='111')
    # s2 = Student(name='222')
    # Student.objects.bulk_create([s1, s2])

    # s = Student.objects.get_or_create(name="张三封")
    # s = Student.objects.update_or_create(defaults={"name":"kkk"},name='张三封')
    # s = Student.objects.get(name='kkk')
    # s.update(name='www')
    # print(type(s))
    # for t in s:
    #     print(t)
    # 查
    # 查询 student 表中 id为2的数据
    # s1 = Student.objects.get(id=2)
    # 查询 student 表中 name 为 zhangsan 的数据
    # get 有一个问题,查询出来的结果 只能有一个,否则就报错
    # s1 = Student.objects.get(name='zhangsan')
    # get查询 最好去查询 字段约束 unique
    # s1 = Student.objects.get(name='zhangsan')
    # students = Student.objects.filter(name="zhangsan")
    # 名字 包含
    # students = Student.objects.filter(name__contains='老')
    # students = Student.objects.filter(name__startswith='李')
    # students = Student.objects.filter(name__endswith='san')

    # 改   根据查询出来的对象,更改其属性,save
    # s1 = Student.objects.get(id=1)
    # s1.name='rose'
    # s1.save()

    # 一次更改多条信息  对 QuerySet
    # Student.objects.filter(name='zhangsan').update(name='张三丰')

    # 删除一个
    # s1 = Student.objects.get(id=1)
    # s1.delete()

    # 删除多个
    # Student.objects.filter(name="张三丰").delete()

    # print(type(students))  # <class 'django.db.models.query.QuerySet'>
    # for student in students:
    #     print(student.id)


    # result = Student.objects.get_or_create(name='王老吉')
    # print(result)
    # result = Student.objects.update_or_create(defaults={'name':'默认值'},name='王老吉')
    # print(result)

    # Student.objects.update(name='未知')

    students = Student.objects.all()
    print(type(students))


    return HttpResponse("ok-------")
